package com.kakarote.examine.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.util.TypeUtils;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kakarote.core.common.Result;
import com.kakarote.core.common.enums.ExamineLabelEnum;
import com.kakarote.core.common.enums.FieldEnum;
import com.kakarote.core.common.enums.SystemCodeEnum;
import com.kakarote.core.entity.BasePage;
import com.kakarote.core.entity.CrmRelationDTO;
import com.kakarote.core.entity.UserInfo;
import com.kakarote.core.exception.CrmException;
import com.kakarote.core.feign.admin.entity.SimpleUser;
import com.kakarote.core.feign.admin.service.AdminService;
import com.kakarote.core.feign.crm.entity.CrmSearchBO;
import com.kakarote.core.feign.crm.entity.SimpleCrmEntity;
import com.kakarote.core.feign.examine.entity.*;
import com.kakarote.core.feign.hrm.HrmFeignService;
import com.kakarote.core.feign.oa.OaService;
import com.kakarote.core.feign.oa.entity.ExamineVO;
import com.kakarote.core.servlet.ApplicationContextHolder;
import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.core.utils.*;
import com.kakarote.examine.common.CommonDateTimeUtil;
import com.kakarote.examine.constant.*;
import com.kakarote.examine.entity.BO.ExaminePageBO;
import com.kakarote.examine.entity.BO.ExaminePreviewBO;
import com.kakarote.examine.entity.PO.ExamineRecordLog;
import com.kakarote.examine.entity.PO.*;
import com.kakarote.examine.entity.VO.ExamineAllVo;
import com.kakarote.examine.entity.VO.SuperExamineFlowDataVO;
import com.kakarote.examine.entity.VO.SuperExamineRecordVO;
import com.kakarote.examine.mapper.ExamineMapper;
import com.kakarote.examine.mapper.SuperExamineRecordLogMapper;
import com.kakarote.examine.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 审批表 服务实现类
 * </p>
 * @author zhangzhiwei
 * @since 2020-11-13
 */
@Service
public class SuperExamineToDoServiceImpl extends BaseServiceImpl<ExamineMapper, Examine> implements ISuperExamineToDoService {

    @Resource IExamineService examineService;
    @Resource IExamineRecordService examineRecordService;
    @Resource IExamineFlowFinalService examineFlowFinalService;
    @Resource ExamineRecordLimitServiceImpl examineRecordLimitService;
    @Resource SuperExamineRecordLogMapper superExamineRecordLogMapper;
    @Resource IExamineRecordLogService examineRecordLogService;
    @Resource ISuperExamineFollowService iSuperExamineFollowService;
    @Resource OaService oaService;
    @Resource AdminService adminService;
    @Resource IExamineFlowService examineFlowService;
    @Resource IExamineFlowOptionalService examineFlowOptionalService;
    @Resource IExamineAdvancedConfigService advancedConfigService;
    @Resource IExamineManagerUserService examineManagerUserService;
    @Resource HrmFeignService hrmService;

    @Override
    public List<Map<String, Object>> getExportField(Long categoryId) {
        Result<List<Map<String, Object>>> field = oaService.getField(categoryId, "1", false);
        List<Integer> excludeFueldId = new ArrayList<Integer>() {{
            add(FieldEnum.ATTACHMENT.getType());
            add(FieldEnum.HANDWRITING_SIGN.getType());
            add(FieldEnum.FILE.getType());
            add(FieldEnum.FIELD_GROUP.getType());
            add(FieldEnum.DESC_TEXT.getType());
        }};
        List<Map<String, Object>> fieldLis = new LinkedList<>();
        if (ObjectUtil.isNotEmpty(field.getData())) {
            List<Map<String, Object>> fieldId = field.getData().stream().filter(e -> !(excludeFueldId.contains(Integer.valueOf(e.get("type").toString())))).collect(Collectors.toList());

            for (Map<String, Object> objectMap : fieldId) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", objectMap.get("fieldName"));
                map.put("name", objectMap.get("name"));
                map.put("sort", fieldId.indexOf(objectMap));
                fieldLis.add(map);
            }


        }
        return fieldLis;
    }

    @Override
    public BasePage<ExamineAllVo> queryAllExamine(ExaminePageBO examinePageBO) {
        if (examinePageBO.getCategoryId() != null) {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        Long userId = null;
        List<Long> roles = null;
        if (!UserUtil.isAdmin()) {
            UserInfo user = UserUtil.getUser();
            userId = user.getUserId();
            roles = user.getRoles();
        }
        String sql = oaSqlMap.get("c");
        if (!UserUtil.isAdmin() || ObjectUtil.isNotEmpty(sql)) {
            List<Long> recordIds = superExamineRecordLogMapper.selectRecordId(userId, roles, sql);
            if (CollectionUtil.isEmpty(recordIds)) {
                recordIds.add(0L);
            }
            examinePageBO.setRecordIdList(recordIds);
        }
        BasePage<ExamineAllVo> basePage = superExamineRecordLogMapper.queryAllExamine(examinePageBO.parse(), examinePageBO, oaSqlMap,userId);
        basePage.getList().forEach(examineAllVo -> {
            examineAllVo.setCreateUserName(UserCacheUtil.getUserName(examineAllVo.getCreateUserId()));
            examineAllVo.setSubmitDeptName(UserCacheUtil.getDeptName(examineAllVo.getSubmitDeptId()));
            //获取创建人信息
            SimpleUser listResult = UserCacheUtil.getSimpleUser(examineAllVo.getCreateUserId());
            examineAllVo.setCreateUser(listResult);
        });
        return basePage;
    }


    @Override
    public List<Map<String, Object>> exportAllExamine(ExaminePageBO examinePageBO) {
        if (examinePageBO.getCategoryId() == null || ObjectUtil.isEmpty(examinePageBO.getFieldList())) {
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        } else {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());

        Long userId = null;
        List<Long> roles = null;
        if (!UserUtil.isAdmin()) {
            UserInfo user = UserUtil.getUser();
            userId = user.getUserId();
            roles = user.getRoles();
        }
        String sql = oaSqlMap.get("c");
        if (!UserUtil.isAdmin() || ObjectUtil.isNotEmpty(sql)) {
            List<Long> recordIds = superExamineRecordLogMapper.selectRecordId(userId, roles, sql);
            if (CollectionUtil.isEmpty(recordIds)) {
                recordIds.add(0L);
            }
            examinePageBO.setRecordIdList(recordIds);
        }

        BasePage<ExamineAllVo> basePage = superExamineRecordLogMapper.queryAllExamine(examinePageBO.parse(), examinePageBO, oaSqlMap,userId);

        List<Long> collect = basePage.getList().stream().map(ExamineAllVo::getExamineId).collect(Collectors.toList());

        return ExportData(collect);
    }


    /**
     * 功能描述: <br>
     * 〈获取待我审批列表数据〉
     *
     * @param:
     * @return: com.kakarote.core.common.Result<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     * @throws:
     * @author: zyh
     * @date: 2022/8/27 11:18
     */
    @Override
    public BasePage<ExamineVO> selectToDoMe(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        if (examinePageBO.getCategoryId() != null) {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabelList(ExamineLabelEnum.allCode());
        //设置为查询待审批
        examinePageBO.setStatus(1);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectRecordLogListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);
        BasePage<ExamineVO> page = new BasePage<>(basePage.getCurrent(), basePage.getSize(), basePage.getTotal());
        Map<Long, ExamineVO>[] examineMap = getExamineMap(basePage);
        List<ExamineVO> examineVoList = new ArrayList<>();
        for (ExamineRecord examineRecord : basePage.getList()) {
            Long typeId = examineRecord.getTypeId();
            if (typeId == null) {
                continue;
            }
            ExamineVO examineVO = getExamineByTypeId(examineMap, examineRecord.getLabel(),typeId);
            if(examineVO==null){
                continue;
            }
            examineVO.setCreateTime(examineRecord.getCreateTime());
            examineVO.setLabel(examineRecord.getLabel());
            SimpleUser lastExamineUser = new SimpleUser();
            List<ExamineRecordLog> hadRecordLogList = examineRecordLogService.lambdaQuery()
                    .eq(ExamineRecordLog::getRecordId, examineRecord.getRecordId())
                    .ne(ExamineRecordLog::getExamineStatus, 3)
                    .orderByDesc(ExamineRecordLog::getCreateTime)
                    .list();
            if (CollectionUtil.isNotEmpty(hadRecordLogList)) {
                ExamineRecordLog examineRecordLog = hadRecordLogList.get(0);
                if (ObjectUtil.isNotEmpty(examineRecordLog.getExamineUserEmail())) {
                    lastExamineUser.setRealname(examineRecordLog.getExamineUserEmail());
                } else if (ObjectUtil.isNotEmpty(examineRecordLog.getType())) {
                    lastExamineUser = UserCacheUtil.getSimpleUser(examineRecordLog.getExamineUserId());
                } else {
                    lastExamineUser = UserCacheUtil.getSimpleUser(examineRecordLog.getCreateUserId());
                }
            }
            //上一步处理人
            examineVO.setLastHandleUser(lastExamineUser);
            //审批停留时间
            List<ExamineRecordLog> havingRecordLogList = examineRecordLogService.lambdaQuery().eq(ExamineRecordLog::getRecordId, examineRecord.getRecordId())
                    .eq(ExamineRecordLog::getExamineStatus, 3)
                    .orderByDesc(ExamineRecordLog::getCreateTime).list();
            if (CollectionUtil.isNotEmpty(havingRecordLogList)) {
                StringBuilder currentUserStr = new StringBuilder();
                for (ExamineRecordLog examineRecordLog : havingRecordLogList) {
                    if (ObjectUtil.isNotEmpty(examineRecordLog.getExamineUserEmail())) {
                        currentUserStr.append(examineRecordLog.getExamineUserEmail()).append(";");
                    } else {
                        currentUserStr.append(UserCacheUtil.getUserName(examineRecordLog.getExamineUserId())).append(";");
                    }
                }
                if (havingRecordLogList.size() == 1) {
                    currentUserStr = new StringBuilder(currentUserStr.toString().replace(";", ""));
                }
                examineVO.setCurrentExamineUser(currentUserStr.toString());
                examineVO.setCurrentStopTime(CommonDateTimeUtil.examineStopTimeStr(havingRecordLogList.get(0).getCreateTime(), LocalDateTime.now()));
            }
            //---------------------------------------
            examineVoList.add(examineVO);
        }
        page.setList(examineVoList);
        return page;
    }


    private Map<Long, ExamineVO>[] getExamineMap (BasePage<ExamineRecord> basePage){
        List<ExamineRecord> recordList = basePage.getList();
        List<ExamineRecord> oaExamines =  recordList.stream().filter(o -> ExamineLabelEnum.getOaCode().contains(o.getLabel())).collect(Collectors.toList());
        List<ExamineRecord> hrmExamines = recordList.stream().filter(o -> ExamineLabelEnum.getHrmCode().contains(o.getLabel())).collect(Collectors.toList());
        List<Long> oaExamineIds =  ExtractUtils.extractField(oaExamines, ExamineRecord::getTypeId);
        List<Long> hrmExamineIds = ExtractUtils.extractField(hrmExamines, ExamineRecord::getTypeId);
        Result<Map<Long, ExamineVO>> oaExmainResult = oaService.getOaExamineByIds(oaExamineIds);
        Result<Map<Long, ExamineVO>> hrExmainResult = hrmService.getHrmExamineByIds(hrmExamineIds);
        Map<Long, ExamineVO> oaExmainMap = oaExmainResult.getData();
        Map<Long, ExamineVO> hrmExamineMap = hrExmainResult.getData();
        Map<Long, ExamineVO>[] result = new Map[]{oaExmainMap,hrmExamineMap};
        return  result;
    }

    private ExamineVO getExamineByTypeId(Map<Long, ExamineVO>[] examineMap,Integer label,Long typeId){
        ExamineVO examineVO = null;
        //OA系统
        if(ExamineLabelEnum.getOaCode().contains(label)){
            examineVO = examineMap[0].get(typeId);
        }
        //HRM系统
        if(ExamineLabelEnum.getHrmCode().contains(label)){
            examineVO = examineMap[1].get(typeId);
        }
        return examineVO;
    }

    @Override
    public List<Map<String, Object>> exportExamineDoMeInfo(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        examinePageBO.setPageType(0);

        if (examinePageBO.getCategoryId() == null || ObjectUtil.isEmpty(examinePageBO.getFieldList())) {
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        } else {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //设置为查询待审批
        examinePageBO.setStatus(1);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectRecordLogListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);


        List<Long> collect = basePage.getList().stream().map(ExamineRecord::getTypeId).collect(Collectors.toList());

        return ExportData(collect);

    }
    @Override
    public List<Map<String, Object>> exportExamineCopyMeInfo(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        examinePageBO.setPageType(0);
        if (examinePageBO.getCategoryId() == null || ObjectUtil.isEmpty(examinePageBO.getFieldList())) {
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        } else {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //设置为查询待审批
        examinePageBO.setStatus(1);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectCopyMeListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);


        List<Long> collect = basePage.getList().stream().map(ExamineRecord::getTypeId).collect(Collectors.toList());

        return ExportData(collect);
    }

    /**
     * 功能描述: <br>
     * 〈获取抄送给我列表数据:0828 需求确定多个抄送节点设置同一个人，相同人员只显示一次抄送信息〉
     * @param:
     * @return: com.kakarote.core.common.Result<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     * @throws:
     * @author: zyh
     * @date: 2022/8/27 11:19
     */
    @Override
    public BasePage<ExamineVO> selectCopyMe(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        if (examinePageBO.getCategoryId() != null) {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabelList(ExamineLabelEnum.allCode());
        //设置为查询待审批
        examinePageBO.setStatus(1);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectCopyMeListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);
        BasePage<ExamineVO> page = new BasePage<>(basePage.getCurrent(), basePage.getSize(), basePage.getTotal());
        Map<Long, ExamineVO>[] examineMap = this.getExamineMap(basePage);
        List<ExamineVO> examineVoList = new ArrayList<>();
        for (ExamineRecord examineRecord : basePage.getList()) {
            Long typeId = examineRecord.getTypeId();
            if (typeId == null) {
                continue;
            }
            ExamineVO examineVO = getExamineByTypeId(examineMap, examineRecord.getLabel(),typeId);
            if(examineVO==null){
                continue;
            }
            examineVO.setCreateTime(examineRecord.getCreateTime());
            examineVO.setLabel(examineRecord.getLabel());
            //---------------------------------------
            SimpleUser lastExamineUser = new SimpleUser();
            List<ExamineRecordLog> hadRecordLogList = examineRecordLogService.lambdaQuery()
                    .eq(ExamineRecordLog::getRecordId, examineRecord.getRecordId())
                    .ne(ExamineRecordLog::getExamineStatus, 3)
                    .orderByDesc(ExamineRecordLog::getCreateTime)
                    .list();
            if (CollectionUtil.isNotEmpty(hadRecordLogList)) {
                ExamineRecordLog examineRecordLog = hadRecordLogList.get(0);
                if (ObjectUtil.isNotEmpty(examineRecordLog.getExamineUserEmail())) {
                    lastExamineUser.setRealname(examineRecordLog.getExamineUserEmail());
                } else if (ObjectUtil.isNotEmpty(examineRecordLog.getType())) {
                    lastExamineUser = UserCacheUtil.getSimpleUser(examineRecordLog.getExamineUserId());
                } else {
                    lastExamineUser = UserCacheUtil.getSimpleUser(examineRecordLog.getCreateUserId());
                }
            }
            //上一步处理人
            examineVO.setLastHandleUser(lastExamineUser);
            //审批停留时间
            List<ExamineRecordLog> havingRecordLogList = examineRecordLogService.lambdaQuery()
                    .eq(ExamineRecordLog::getRecordId, examineRecord.getRecordId())
                    .eq(ExamineRecordLog::getExamineStatus, 3)
                    .orderByDesc(ExamineRecordLog::getCreateTime)
                    .list();
            if (CollectionUtil.isNotEmpty(havingRecordLogList)) {
                examineVO.setCurrentStopTime(CommonDateTimeUtil.examineStopTimeStr(havingRecordLogList.get(0).getCreateTime(), LocalDateTime.now()));
            }
            //---------------------------------------
            examineVoList.add(examineVO);
        }
        page.setList(examineVoList);
        return page;
    }

    /**
     * 功能描述: <br>
     * 〈获取待我审批列表数据〉归档-我申请的
     *
     * @param:
     * @return: com.kakarote.core.common.Result<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     * @throws:
     * @author: zyh
     * @date: 2022/8/27 11:18
     */
    @Override
    public BasePage<ExamineVO> selectEndMe(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        if (examinePageBO.getCategoryId() != null) {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabelList(ExamineLabelEnum.allCode());
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectEndMeListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);
        BasePage<ExamineVO> page = new BasePage<>(basePage.getCurrent(), basePage.getSize(), basePage.getTotal());
        Map<Long, ExamineVO>[] examineMap = this.getExamineMap(basePage);
        List<ExamineVO> examineVoList = Lists.newArrayList();
        for (ExamineRecord examineRecord : basePage.getList()) {
            Long typeId = examineRecord.getTypeId();
            if (typeId == null) {
                continue;
            }
            ExamineVO examineVO = this.getExamineByTypeId(examineMap, examineRecord.getLabel(), typeId);
            examineVO.setCreateTime(examineRecord.getCreateTime());
            examineVO.setLabel(examineRecord.getLabel());
            //0917添加关注
            List<ExamineFollow> followList = iSuperExamineFollowService.lambdaQuery().eq(ExamineFollow::getFollowUserId, UserUtil.getUserId()).eq(ExamineFollow::getExamineId, examineRecord.getExamineId()).eq(ExamineFollow::getRecordId, examineRecord.getRecordId()).list();
            if (CollectionUtil.isNotEmpty(followList)) {
                examineVO.setFollowStatus(1);
            }
            examineVoList.add(examineVO);
        }
        page.setList(examineVoList);
        return page;
    }


    /**
     * 归档我的申请导出
     *
     * @param examinePageBO
     * @return
     */
    @Override
    public List<Map<String, Object>> exportExamineEenMeInfo(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        examinePageBO.setPageType(0);
        if (examinePageBO.getCategoryId() == null || ObjectUtil.isEmpty(examinePageBO.getFieldList())) {
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        } else {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectEndMeListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);

        List<Long> collect = basePage.getList().stream().map(ExamineRecord::getTypeId).collect(Collectors.toList());

        return ExportData(collect);
    }

    /**
     * 功能描述: <br>
     * 〈获取待我审批列表数据〉归档-经我审批
     *
     * @param:
     * @return: com.kakarote.core.common.Result<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     * @throws:
     * @author: zyh
     * @date: 2022/8/27 11:18
     */
    @Override
    public BasePage<ExamineVO> selectEndAudit(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        if (examinePageBO.getCategoryId() != null) {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectEndAuditListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);
        BasePage<ExamineVO> page = new BasePage<>(basePage.getCurrent(), basePage.getSize(), basePage.getTotal());
        List<ExamineVO> examineVoList = new ArrayList<>();
        for (ExamineRecord examineRecord : basePage.getList()) {
            Long typeId = examineRecord.getTypeId();
            if (typeId == null) {
                continue;
            }
            ExamineVO examineVO = oaService.getOaExamineById(typeId).getData();
            examineVO.setCreateTime(examineRecord.getCreateTime());

            //----------------------------------------
            //0917添加关注
            List<ExamineFollow> followList = iSuperExamineFollowService.lambdaQuery().eq(ExamineFollow::getFollowUserId, UserUtil.getUserId()).eq(ExamineFollow::getExamineId, examineRecord.getExamineId()).eq(ExamineFollow::getRecordId, examineRecord.getRecordId()).list();
            if (CollectionUtil.isNotEmpty(followList)) {
                examineVO.setFollowStatus(1);
            }
            //----------------------------------------
            examineVoList.add(examineVO);
        }
        page.setList(examineVoList);
        return page;
    }


    /**
     * 归档经我审批导出
     *
     * @param examinePageBO
     * @return
     */
    @Override
    public List<Map<String, Object>> exportExamineEenAuditInfo(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        examinePageBO.setPageType(0);
        if (examinePageBO.getCategoryId() == null || ObjectUtil.isEmpty(examinePageBO.getFieldList())) {
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        } else {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        examinePageBO.setLabel(0);
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectEndAuditListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);

        List<Long> collect = basePage.getList().stream().map(ExamineRecord::getTypeId).collect(Collectors.toList());

        return ExportData(collect);
    }

    /**
     * 功能描述: <br>
     * 〈获取待我审批列表数据〉归档-我关注的
     *
     * @param:
     * @return: com.kakarote.core.common.Result<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     * @throws:
     * @author: zyh
     * @date: 2022/8/27 11:18
     */
    @Override
    public BasePage<ExamineVO> selectEndFollow(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        if (examinePageBO.getCategoryId() != null) {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectEndFollowListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);
        BasePage<ExamineVO> page = new BasePage<>(basePage.getCurrent(), basePage.getSize(), basePage.getTotal());
        List<ExamineVO> examineVoList = new ArrayList<>();
        for (ExamineRecord examineRecord : basePage.getList()) {
            Long typeId = examineRecord.getTypeId();
            if (typeId == null) {
                continue;
            }
            ExamineVO examineVO = oaService.getOaExamineById(typeId).getData();
            examineVO.setCreateTime(examineRecord.getCreateTime());
            //----------------------------------------
            //0917添加关注
            List<ExamineFollow> followList = iSuperExamineFollowService.lambdaQuery().eq(ExamineFollow::getFollowUserId, UserUtil.getUserId()).eq(ExamineFollow::getExamineId, examineRecord.getExamineId()).eq(ExamineFollow::getRecordId, examineRecord.getRecordId()).list();
            if (CollectionUtil.isNotEmpty(followList)) {
                examineVO.setFollowStatus(1);
            }
            //----------------------------------------
            examineVoList.add(examineVO);
        }
        page.setList(examineVoList);
        return page;
    }


    /**
     * 归档我的关注导出
     *
     * @param examinePageBO
     * @return
     */
    @Override
    public List<Map<String, Object>> exportExamineEenFollowInfo(ExaminePageBO examinePageBO) {

        UserInfo user = UserUtil.getUser();
        examinePageBO.setPageType(0);
        if (examinePageBO.getCategoryId() == null || ObjectUtil.isEmpty(examinePageBO.getFieldList())) {
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        } else {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectEndFollowListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);

        List<Long> collect = basePage.getList().stream().map(ExamineRecord::getTypeId).collect(Collectors.toList());

        return ExportData(collect);
    }

    /**
     * 功能描述: <br>
     * 〈获取待我审批列表数据〉归档-作废的
     *
     * @param:
     * @return: com.kakarote.core.common.Result<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     * @throws:
     * @author: zyh
     * @date: 2022/8/27 11:18
     */
    @Override
    public BasePage<ExamineVO> selectEndToVoid(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        if (examinePageBO.getCategoryId() != null) {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectEndToVoidListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);
        BasePage<ExamineVO> page = new BasePage<>(basePage.getCurrent(), basePage.getSize(), basePage.getTotal());
        List<ExamineVO> examineVoList = new ArrayList<>();
        for (ExamineRecord examineRecord : basePage.getList()) {
            Long typeId = examineRecord.getTypeId();
            if (typeId == null) {
                continue;
            }
            ExamineVO examineVO = oaService.getOaExamineById(typeId).getData();
            examineVO.setCreateTime(examineRecord.getCreateTime());

            //----------------------------------------
            //0917添加关注
            List<ExamineFollow> followList = iSuperExamineFollowService.lambdaQuery().eq(ExamineFollow::getFollowUserId, UserUtil.getUserId()).eq(ExamineFollow::getExamineId, examineRecord.getExamineId()).eq(ExamineFollow::getRecordId, examineRecord.getRecordId()).list();
            if (CollectionUtil.isNotEmpty(followList)) {
                examineVO.setFollowStatus(1);
            }
            //----------------------------------------
            examineVoList.add(examineVO);
        }
        page.setList(examineVoList);
        return page;
    }


    /**
     * 导出归档作废的
     *
     * @param examinePageBO
     * @return
     */
    @Override
    public List<Map<String, Object>> exportExamineEenTovoIdInfo(ExaminePageBO examinePageBO) {

        UserInfo user = UserUtil.getUser();
        examinePageBO.setPageType(0);
        if (examinePageBO.getCategoryId() == null || ObjectUtil.isEmpty(examinePageBO.getFieldList())) {
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        } else {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectEndToVoidListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);

        List<Long> collect = basePage.getList().stream().map(ExamineRecord::getTypeId).collect(Collectors.toList());

        return ExportData(collect);
    }

    /**
     * 功能描述: <br>
     * 〈获取待我审批列表数据〉归档-作废的
     *
     * @param:
     * @return: com.kakarote.core.common.Result<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     * @throws:
     * @author: zyh
     * @date: 2022/8/27 11:18
     */
    @Override
    public BasePage<ExamineVO> selectEndCopy(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        if (examinePageBO.getCategoryId() != null) {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectEndCopyListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);
        BasePage<ExamineVO> page = new BasePage<>(basePage.getCurrent(), basePage.getSize(), basePage.getTotal());
        List<ExamineVO> examineVoList = new ArrayList<>();
        for (ExamineRecord examineRecord : basePage.getList()) {
            Long typeId = examineRecord.getTypeId();
            if (typeId == null) {
                continue;
            }
            ExamineVO examineVO = oaService.getOaExamineById(typeId).getData();
            examineVO.setCreateTime(examineRecord.getCreateTime());

            //----------------------------------------
            //0917添加关注
            List<ExamineFollow> followList = iSuperExamineFollowService.lambdaQuery().eq(ExamineFollow::getFollowUserId, UserUtil.getUserId()).eq(ExamineFollow::getExamineId, examineRecord.getExamineId()).eq(ExamineFollow::getRecordId, examineRecord.getRecordId()).list();
            if (CollectionUtil.isNotEmpty(followList)) {
                examineVO.setFollowStatus(1);
            }
            //----------------------------------------
            examineVoList.add(examineVO);
        }
        page.setList(examineVoList);
        return page;
    }


    @Override
    public List<Map<String, Object>> exportExamineEenCopyInfo(ExaminePageBO examinePageBO) {

        UserInfo user = UserUtil.getUser();
        examinePageBO.setPageType(0);
        if (examinePageBO.getCategoryId() == null || ObjectUtil.isEmpty(examinePageBO.getFieldList())) {
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        } else {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectEndCopyListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);

        List<Long> collect = basePage.getList().stream().map(ExamineRecord::getTypeId).collect(Collectors.toList());

        return ExportData(collect);
    }


    @Override
    public SuperExamineRecordVO queryExamineRecordList(Long recordId, Long ownerUserId, String outExamineEmail, Integer outExamineStyle) {
        SuperExamineRecordVO examineRecordVO = new SuperExamineRecordVO();
        ExamineRecord examineRecord = examineRecordService.getById(recordId);
        if (ObjectUtil.isEmpty(examineRecord)) {
            return examineRecordVO;
        }
        examineRecordVO.setExamineStatus(examineRecord.getExamineStatus());

        //设置业务类型label
        Integer label = examineRecord.getLabel();
        examineRecordVO.setLabel(label);
        //设置审批创建人
        setCreateUser(examineRecordVO, examineRecord.getCreateUserId());
        //设置配置信息
        setAdvancedConfig(examineRecordVO, examineRecord);
        //设置审批流程
        setExamineFlowList(examineRecordVO, examineRecord);
        //设置自选条件
        setOptionalCondition(examineRecordVO);

        //设置是否有超管权限
        setIsSuperAdmin(examineRecordVO, examineRecord);

        //处理没有高级配置的情况
        examineRecordVO.getExamineAdvancedConfigVO().setExamineId(examineRecord.getExamineId());

        if (ObjectUtil.equal(examineRecordVO.getIsSuperAdmin(), 1) || UserUtil.isAdmin()) {
            //设置跳过、恢复权限
            if (Objects.equals(ExamineStatusEnum.PASS.getStatus(), examineRecord.getExamineStatus())) {
                examineRecordVO.setIsRestore(1);
            } else if (Arrays.asList(ExamineStatusEnum.AWAIT.getStatus(), ExamineStatusEnum.UNDERWAY.getStatus()).contains(examineRecord.getExamineStatus())) {
                examineRecordVO.setIsSkip(1);
            }
        }

        //添加如果是归档里的审批请求， 禁用掉返回的按钮
        if (label.equals(0)) {
            ExamineVO oaExamine = oaService.getOaExamineById(examineRecord.getTypeId()).getData();
            if (ObjectUtil.isNotEmpty(oaExamine) && Arrays.asList(3, 4).contains(oaExamine.getExamineFlag())) {
                examineRecordVO.setIsCheck(0);
                examineRecordVO.setIsRecheck(0);
            } else {
                //设置是否有审批权限
                setIsCheck(examineRecordVO, recordId);
            }
        } else {
            //设置是否有审批权限
            setIsCheck(examineRecordVO, recordId);
        }
        //0923 添加外部联系人审批过后   禁用掉该外部审批人调用的后续审批接口
        if (ObjectUtil.isNotEmpty(outExamineEmail) && outExamineStyle.equals(2)) {
            List<ExamineRecordLog> checkRecordLogs = examineRecordLogService.lambdaQuery()
                    .eq(ExamineRecordLog::getRecordId, recordId)
                    .eq(ExamineRecordLog::getExamineUserEmail, outExamineEmail)
                    .eq(ExamineRecordLog::getExamineStatus, ExamineStatusEnum.UNDERWAY.getStatus()).list();
            if (CollectionUtil.isEmpty(checkRecordLogs)) {
                examineRecordVO.setIsCheck(0);
                examineRecordVO.setIsRecheck(0);
            } else {
                //设置 外部联系人  获取审批页面时候 如果没有审核通过 设置可以审核
                examineRecordVO.setIsCheck(1);
            }
        }
        return examineRecordVO;
    }


    private void setOptionalCondition(SuperExamineRecordVO examineRecordVO) {

        if (CollectionUtil.isEmpty(examineRecordVO.getExamineFlowList())) {
            return;
        }
        examineRecordVO.getExamineFlowList().forEach(e -> {

            if (ObjectUtil.isNotEmpty(e.getUserList())) {
                return;
            }
            ExamineTypeEnum examineTypeEnum = ExamineTypeEnum.valueOf(e.getExamineType());
            switch (examineTypeEnum) {
                case OPTIONAL:
                    IExamineFlowOptionalService flowOptionalService = ApplicationContextHolder.getBean(IExamineFlowOptionalService.class);
                    List<ExamineFlowOptional> list = flowOptionalService.lambdaQuery().eq(ExamineFlowOptional::getFlowId, e.getFlowId()).list();

                    Optional<ExamineFlowOptional> first = list.stream().findFirst();

                    if (!first.isPresent()) {
                        return;
                    }
                    ExamineFlowOptional examineFlowOptional = first.get();
                    List<Long> userIds = list.stream()
                            .map(ExamineFlowOptional::getUserId)
                            .filter(ObjectUtil::isNotEmpty)
                            .collect(Collectors.toList());

                    List<Map<String, Object>> userList = new ArrayList<>();
                    for (Long userId : userIds) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("userId", userId);
                        map.put("realname", UserCacheUtil.getUserName(userId));
                        userList.add(map);
                    }

                    String roleId = examineFlowOptional.getRoleId();
                    if (StrUtil.isNotEmpty(roleId)) {
                        List<Long> roleIds = StrUtil.split(roleId, ',', -1, true, Long::valueOf);
                        adminService.queryUserIdByRoleIds(roleIds).getData().forEach(o -> {
                            Map<String, Object> map = new HashMap<>();
                            map.put("userId", o);
                            map.put("realname", UserCacheUtil.getUserName(o));
                            userList.add(map);
                        });
                    }

                    e.setChooseUserList(userList);
                    e.setChooseType(examineFlowOptional.getChooseType());
                    e.setRangeType(examineFlowOptional.getRangeType());
                    e.setRoleId(examineFlowOptional.getRoleId());
                    break;
                case COPY:

                    IExamineFlowCopyService flowCopyService = ApplicationContextHolder.getBean(IExamineFlowCopyService.class);
                    ExamineFlowCopy examineFlowCopy = flowCopyService.lambdaQuery().eq(ExamineFlowCopy::getFlowId, e.getFlowId()).one();
                    if (examineFlowCopy == null) {
                        return;
                    }
                    e.setIsAdd(examineFlowCopy.getIsAdd());
                    break;
            }


        });

    }


    private void setIsSuperAdmin(SuperExamineRecordVO examineRecordVO, ExamineRecord examineRecord) {
        ExamineManagerUser managerUser = examineManagerUserService.lambdaQuery()
                .eq(ExamineManagerUser::getExamineId, examineRecord.getExamineId())
                .eq(ExamineManagerUser::getUserId, UserUtil.getUserId()).one();

        examineRecordVO.setIsSuperAdmin(ObjectUtil.isEmpty(managerUser) ? 0 : 1);

    }

    /**
     * 跟踪-我申请的
     */
    @Override
    public BasePage<ExamineVO> selectTrackMe(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        if (examinePageBO.getCategoryId() != null) {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询所有审批，包含业务和OA审批
        examinePageBO.setLabelList(ExamineLabelEnum.allCode());
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectTrackMeListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);
        BasePage<ExamineVO> page = new BasePage<>(basePage.getCurrent(), basePage.getSize(), basePage.getTotal());
        List<ExamineVO> examineVoList = new ArrayList<>();
        Map<Long, ExamineVO>[] examineMap = getExamineMap(basePage);
        for (ExamineRecord examineRecord : basePage.getList()) {
            Long typeId = examineRecord.getTypeId();
            if (typeId == null) {
                continue;
            }
            ExamineVO examineVO = getExamineByTypeId(examineMap, examineRecord.getLabel(),typeId);
            if(examineVO==null){
                continue;
            }
            examineVO.setCreateTime(examineRecord.getCreateTime());
            examineVO.setLabel(examineRecord.getLabel());
            //0917添加关注
            List<ExamineFollow> followList = iSuperExamineFollowService.lambdaQuery().eq(ExamineFollow::getFollowUserId, UserUtil.getUserId()).eq(ExamineFollow::getExamineId, examineRecord.getExamineId()).eq(ExamineFollow::getRecordId, examineRecord.getRecordId()).list();
            if (CollectionUtil.isNotEmpty(followList)) {
                examineVO.setFollowStatus(1);
            }
            //审批停留时间
            List<ExamineRecordLog> havingRecordLogList = examineRecordLogService.lambdaQuery()
                    .eq(ExamineRecordLog::getRecordId, examineRecord.getRecordId())
                    .eq(ExamineRecordLog::getExamineStatus, 3)
                    .orderByDesc(ExamineRecordLog::getCreateTime)
                    .list();
            if (CollectionUtil.isNotEmpty(havingRecordLogList)) {
                StringBuilder currentUserStr = new StringBuilder();
                for (ExamineRecordLog examineRecordLog : havingRecordLogList) {
                    if (ObjectUtil.isNotEmpty(examineRecordLog.getExamineUserEmail())) {
                        currentUserStr.append(examineRecordLog.getExamineUserEmail()).append(";");
                    } else {
                        currentUserStr.append(UserCacheUtil.getUserName(examineRecordLog.getExamineUserId())).append(";");
                    }
                }
                if (havingRecordLogList.size() == 1) {
                    currentUserStr = new StringBuilder(currentUserStr.toString().replace(";", ""));
                }
                examineVO.setCurrentExamineUser(currentUserStr.toString());
                examineVO.setCurrentStopTime(CommonDateTimeUtil.examineStopTimeStr(havingRecordLogList.get(0).getCreateTime(), LocalDateTime.now()));
            }
            //---------------------------------------
            examineVoList.add(examineVO);
        }
        page.setList(examineVoList);
        return page;
    }


    @Override
    public List<Map<String, Object>> exportExamineTrackMeInfo(ExaminePageBO examinePageBO) {

        UserInfo user = UserUtil.getUser();
        examinePageBO.setPageType(0);
        if (examinePageBO.getCategoryId() == null || ObjectUtil.isEmpty(examinePageBO.getFieldList())) {
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        } else {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectTrackMeListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);

        List<Long> collect = basePage.getList().stream().map(ExamineRecord::getTypeId).collect(Collectors.toList());

        return ExportData(collect);
    }

    /**
     * 跟踪-经我审批的
     */
    @Override
    public BasePage<ExamineVO> selectTrackDo(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        if (examinePageBO.getCategoryId() != null) {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabelList(ExamineLabelEnum.allCode());
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectTrackDoListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);
        BasePage<ExamineVO> page = new BasePage<>(basePage.getCurrent(), basePage.getSize(), basePage.getTotal());
        Map<Long, ExamineVO>[] examineMap = this.getExamineMap(basePage);
        List<ExamineVO> examineVoList = new ArrayList<>();
        for (ExamineRecord examineRecord : basePage.getList()) {
            Long typeId = examineRecord.getTypeId();
            if (typeId == null) {
                continue;
            }
            ExamineVO examineVO = this.getExamineByTypeId(examineMap, examineRecord.getLabel(), typeId);
            examineVO.setCreateTime(examineRecord.getCreateTime());
            examineVO.setLabel(examineRecord.getLabel());
            List<ExamineFollow> followList = iSuperExamineFollowService.lambdaQuery().eq(ExamineFollow::getFollowUserId, UserUtil.getUserId()).eq(ExamineFollow::getExamineId, examineRecord.getExamineId()).eq(ExamineFollow::getRecordId, examineRecord.getRecordId()).list();
            if (CollectionUtil.isNotEmpty(followList)) {
                examineVO.setFollowStatus(1);
            }
            //审批停留时间
            List<ExamineRecordLog> havingRecordLogList = examineRecordLogService.lambdaQuery()
                    .eq(ExamineRecordLog::getRecordId, examineRecord.getRecordId())
                    .eq(ExamineRecordLog::getExamineStatus, 3)
                    .orderByDesc(ExamineRecordLog::getCreateTime)
                    .list();
            if (CollectionUtil.isNotEmpty(havingRecordLogList)) {
                StringBuilder currentUserStr = new StringBuilder();
                for (ExamineRecordLog examineRecordLog : havingRecordLogList) {
                    if (ObjectUtil.isNotEmpty(examineRecordLog.getExamineUserEmail())) {
                        currentUserStr.append(examineRecordLog.getExamineUserEmail()).append(";");
                    } else {
                        currentUserStr.append(UserCacheUtil.getUserName(examineRecordLog.getExamineUserId())).append(";");
                    }
                }
                if (havingRecordLogList.size() == 1) {
                    currentUserStr = new StringBuilder(currentUserStr.toString().replace(";", ""));
                }
                examineVO.setCurrentExamineUser(currentUserStr.toString());
                examineVO.setCurrentStopTime(CommonDateTimeUtil.examineStopTimeStr(havingRecordLogList.get(0).getCreateTime(), LocalDateTime.now()));
            }
            //---------------------------------------

            examineVoList.add(examineVO);
        }
        page.setList(examineVoList);
        return page;
    }
    @Override
    public List<Map<String, Object>> exportExamineTrackDoInfo(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        examinePageBO.setPageType(0);
        if (examinePageBO.getCategoryId() == null || ObjectUtil.isEmpty(examinePageBO.getFieldList())) {
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        } else {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());


        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectTrackDoListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);

        List<Long> collect = basePage.getList().stream().map(ExamineRecord::getTypeId).collect(Collectors.toList());

        return ExportData(collect);
    }
    /**
     * 跟踪-抄送给我的
     */
    @Override
    public BasePage<ExamineVO> selectTrackCopy(ExaminePageBO examinePageBO) {
        UserInfo user = UserUtil.getUser();
        if (examinePageBO.getCategoryId() != null) {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        examinePageBO.setLabelList(ExamineLabelEnum.allCode());
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());
        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectTrackCopyListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);
        BasePage<ExamineVO> page = new BasePage<>(basePage.getCurrent(), basePage.getSize(), basePage.getTotal());
        Map<Long, ExamineVO>[] examineMap = this.getExamineMap(basePage);

        List<ExamineVO> examineVoList = new ArrayList<>();
        for (ExamineRecord examineRecord : basePage.getList()) {
            Long typeId = examineRecord.getTypeId();
            if (typeId == null) {
                continue;
            }
            ExamineVO examineVO = getExamineByTypeId(examineMap, examineRecord.getLabel(),typeId);
            if(examineVO==null){
                continue;
            }
            examineVO.setCreateTime(examineRecord.getCreateTime());
            examineVO.setLabel(examineRecord.getLabel());
            List<ExamineFollow> followList = iSuperExamineFollowService.lambdaQuery().eq(ExamineFollow::getFollowUserId, UserUtil.getUserId()).eq(ExamineFollow::getExamineId, examineRecord.getExamineId()).eq(ExamineFollow::getRecordId, examineRecord.getRecordId()).list();
            if (CollectionUtil.isNotEmpty(followList)) {
                examineVO.setFollowStatus(1);
            }
            //审批停留时间
            List<ExamineRecordLog> havingRecordLogList = examineRecordLogService.lambdaQuery()
                    .eq(ExamineRecordLog::getRecordId, examineRecord.getRecordId())
                    .eq(ExamineRecordLog::getExamineStatus, 3)
                    .orderByDesc(ExamineRecordLog::getCreateTime)
                    .list();
            if (CollectionUtil.isNotEmpty(havingRecordLogList)) {
                StringBuilder currentUserStr = new StringBuilder();
                for (ExamineRecordLog examineRecordLog : havingRecordLogList) {
                    if (ObjectUtil.isNotEmpty(examineRecordLog.getExamineUserEmail())) {
                        currentUserStr.append(examineRecordLog.getExamineUserEmail()).append(";");
                    } else {
                        currentUserStr.append(UserCacheUtil.getUserName(examineRecordLog.getExamineUserId())).append(";");
                    }
                }
                if (havingRecordLogList.size() == 1) {
                    currentUserStr = new StringBuilder(currentUserStr.toString().replace(";", ""));
                }
                examineVO.setCurrentExamineUser(currentUserStr.toString());
                examineVO.setCurrentStopTime(CommonDateTimeUtil.examineStopTimeStr(havingRecordLogList.get(0).getCreateTime(), LocalDateTime.now()));
            }
            //---------------------------------------
            examineVoList.add(examineVO);
        }
        page.setList(examineVoList);
        return page;
    }


    @Override
    public List<Map<String, Object>> exportExamineTrackCopyInfo(ExaminePageBO examinePageBO) {

        UserInfo user = UserUtil.getUser();
        examinePageBO.setPageType(0);
        if (examinePageBO.getCategoryId() == null || ObjectUtil.isEmpty(examinePageBO.getFieldList())) {
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        } else {
            Examine examine = getById(examinePageBO.getCategoryId());
            examinePageBO.setCategoryId(examine != null ? examine.getExamineInitId() : null);
        }
        //设置为查询oa审批
        examinePageBO.setLabel(0);
        //设置为查询待审批
        examinePageBO.setStatus(2);
        //Bi时间格式转换
        setBiTime(examinePageBO);
        Map<String, String> oaSqlMap = getOaSqlMap(examinePageBO.getSearchBO());


        BasePage<ExamineRecord> basePage = superExamineRecordLogMapper.selectTrackCopyListByUser(examinePageBO.parse(), examinePageBO, user.getUserId(), user.getRoles(), oaSqlMap);

        List<Long> collect = basePage.getList().stream().map(ExamineRecord::getTypeId).collect(Collectors.toList());

        return ExportData(collect);
    }


    /**
     * 功能描述: <br>
     * 〈设置审批节点已读〉
     *
     * @param: recordId
     * userId
     * outExamineEmail
     * examineRecordTypeIdStyle
     * @return: void
     * @throws:
     * @author: zyh
     * @date: 2022/10/29 14:51
     */
    @Override
    public void setRecordLogRead(Long recordId, Long userId, String outExamineEmail, Integer examineRecordTypeIdStyle) {
        LambdaUpdateWrapper<ExamineRecordLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ExamineRecordLog::getRecordId, recordId);
        if (2 == examineRecordTypeIdStyle) {
            updateWrapper.eq(ExamineRecordLog::getExamineUserEmail, outExamineEmail);
        } else {
            updateWrapper.eq(ExamineRecordLog::getExamineUserId, userId);
        }
        updateWrapper.and(wrapper -> {
            wrapper.or(newWrapper -> newWrapper.ne(ExamineRecordLog::getReadFlag, ReadFlagEnum.READ.getType()));
            wrapper.or(newWrapper -> newWrapper.isNull(ExamineRecordLog::getReadFlag));
        });

        ExamineRecordLog recordLog = new ExamineRecordLog();
        recordLog.setReadFlag(ReadFlagEnum.READ.getType());
        examineRecordLogService.update(recordLog, updateWrapper);
    }


    private SuperExamineRecordVO setIsRecheck(SuperExamineRecordVO superExamineRecordVO, ExamineRecord examineRecord) {
        //当前审批人
        Long auditUserId = UserUtil.getUserId();
        //设置是否有撤回权限
        boolean isValidUser = auditUserId.equals(examineRecord.getCreateUserId());
        boolean isNormalStatus = (examineRecord.getExamineStatus().equals(ExamineStatusEnum.AWAIT.getStatus()) || examineRecord.getExamineStatus().equals(ExamineStatusEnum.UNDERWAY.getStatus()));
        Boolean isCreateUserWithdraw = superExamineRecordVO.getExamineAdvancedConfigVO().getIsCreateUserWithdraw();
        if (isCreateUserWithdraw == null) {
            isCreateUserWithdraw = false;
        }
        if (isValidUser && isNormalStatus && isCreateUserWithdraw) {
            superExamineRecordVO.setIsRecheck(1);
        } else {
            superExamineRecordVO.setIsRecheck(0);
        }
        return superExamineRecordVO;
    }

    private SuperExamineRecordVO setIsCheck(SuperExamineRecordVO superExamineRecordVO, Long recordId) {
        //获取当前登录人
        Long auditUserId = UserUtil.getUserId();
        //获取审批日志
        List<ExamineRecordLog> recordLogs = examineRecordLogService.lambdaQuery()
                .eq(ExamineRecordLog::getRecordId, recordId)
                .eq(ExamineRecordLog::getExamineStatus, ExamineStatusEnum.UNDERWAY.getStatus()).list();
        List<Long> examineUserIds = new ArrayList<>();
        if (CollUtil.isNotEmpty(recordLogs)) {
            examineUserIds = recordLogs.stream().map(ExamineRecordLog::getExamineUserId).collect(Collectors.toList());
        }
        //当前登录人是否有审核权限
        if (examineUserIds.contains(auditUserId)) {
            superExamineRecordVO.setIsCheck(1);
        } else {
            int isCheck = 0;
            superExamineRecordVO.setIsCheck(isCheck);
        }
        return superExamineRecordVO;
    }

    private SuperExamineRecordVO setCreateUser(SuperExamineRecordVO superExamineRecordVO, Long userId) {
        SimpleUser createUser = UserCacheUtil.getSimpleUser(userId);
        superExamineRecordVO.setCreateUser(createUser);
        return superExamineRecordVO;
    }

    private SuperExamineRecordVO setExamineFlowList(SuperExamineRecordVO superExamineRecordVO, ExamineRecord examineRecord) {
        //获取所有的flow
        List<ExamineFlowVO> examineFlowVoS = this.getExamineFlowVOs(examineRecord);
        Map<Long, List<ExamineFlowVO>> examineFlowFinalListMap = examineFlowVoS.stream().collect(Collectors.groupingBy(ExamineFlowVO::getFlowId));
        /**
         * 对节点进行排序
         */
        List<ExamineFlowVO> examineFlowVoSSort = new ArrayList<>();
        List<ExamineFlowFinal> examineFlowFinalList = examineFlowFinalService.lambdaQuery()
                .eq(ExamineFlowFinal::getRecordId, examineRecord.getRecordId())
                .orderByAsc(ExamineFlowFinal::getSort, ExamineFlowFinal::getCreateTime)
                .list();
        //对老数据兼容
        if (CollectionUtil.isEmpty(examineFlowFinalList)) {
            List<ExamineFlow> examineFlows = examineFlowService.lambdaQuery().eq(ExamineFlow::getExamineId, examineRecord.getExamineId()).orderByAsc(ExamineFlow::getSort).list();
            if (CollectionUtil.isNotEmpty(examineFlows)) {
                for (ExamineFlow examineFlow : examineFlows) {
                    List<ExamineFlowOptional> flowOptionals = examineFlowOptionalService.lambdaQuery().eq(ExamineFlowOptional::getFlowId, examineFlow.getFlowId()).list();
                    ExamineFlowFinal examineFlowFinal = BeanUtil.copyProperties(examineFlow, ExamineFlowFinal.class);
                    examineFlowFinal.setRecordId(examineRecord.getRecordId());
                    if (CollectionUtil.isNotEmpty(flowOptionals)) {
                        examineFlowFinal.setType(CollectionUtil.getFirst(flowOptionals).getType());
                    }
                    examineFlowFinalList.add(examineFlowFinal);
                }
            }
        }

        for (ExamineFlowFinal examineFlowFinal : examineFlowFinalList) {
            if (ObjectUtil.isEmpty(examineFlowFinal.getType())) {
                examineFlowFinal.setType(1);
            }
            List<ExamineFlowVO> superExamineFlowDataVOS = examineFlowFinalListMap.get(examineFlowFinal.getFlowId());
            if (CollectionUtil.isNotEmpty(superExamineFlowDataVOS)) {
                ExamineFlowVO examineFlowVO = superExamineFlowDataVOS.get(0);
                if (ObjectUtil.isEmpty(examineFlowVO.getType())) {
                    examineFlowVO.setType(1);
                }
                examineFlowVoSSort.add(examineFlowVO);
            }
        }
        ExamineFlowFinal flowFinal;

        List<Long> collect = examineFlowFinalList.stream().map(ExamineFlowFinal::getFlowId).collect(Collectors.toList());

        List<ExamineRecordLog> recordLogs = examineRecordLogService.lambdaQuery().in(ExamineRecordLog::getFlowId, collect).eq(ExamineRecordLog::getExamineStatus, ExamineStatusEnum.UNDERWAY.getStatus()).list();

        if (CollectionUtil.isNotEmpty(recordLogs)) {
            flowFinal = examineFlowFinalList.stream().filter(examineFlowFinal -> examineFlowFinal.getFlowId().equals(recordLogs.get(0).getFlowId())).findFirst().orElse(null);
        } else {
            // 获取examineFlowFinalList最后一个
            flowFinal = examineFlowFinalList.get(examineFlowFinalList.size() - 1);
        }

        //封装DataVos
        List<SuperExamineFlowDataVO> examineFlowDataVoS = this.getExamineFlowDataVO(superExamineRecordVO, examineFlowVoSSort, examineRecord, flowFinal);
        //补充征求他人意见节点
        examineFlowDataVoS = getSeekExamineFlowDataVO(examineFlowDataVoS, examineFlowVoSSort, examineRecord.getRecordId());
        //补充创建人节点
        SuperExamineFlowDataVO examineFlowDataVO = this.supplementExamineFlowDataVO(superExamineRecordVO.getCreateUser(), examineRecord.getCreateTime());
        examineFlowDataVoS.add(0, examineFlowDataVO);

        superExamineRecordVO.setExamineFlowList(examineFlowDataVoS);
        return superExamineRecordVO;
    }

    private SuperExamineRecordVO setAdvancedConfig(SuperExamineRecordVO superExamineRecordVO, ExamineRecord examineRecord) {
        //获取审批配置信息
        ExamineAdvancedConfigVO examineAdvancedConfigVO = examineService.queryAdvanceConfig(examineRecord.getExamineId());
        superExamineRecordVO.setExamineAdvancedConfigVO(examineAdvancedConfigVO);
        //设置发起人是否有撤回权限
        setIsRecheck(superExamineRecordVO, examineRecord);
        return superExamineRecordVO;
    }


    private List<ExamineFlowVO> getExamineFlowVOs(ExamineRecord examineRecord) {
        ExaminePreviewBO examinePreviewBO = new ExaminePreviewBO();
        examinePreviewBO.setLabel(examineRecord.getLabel());
        Map<String, Object> dataMap = new HashMap<>(8);
        List<ExamineFlowConditionDataVO> conditionDataVoS = examineService.previewFiledName(examineRecord.getLabel(), null, examineRecord.getExamineId());
        //判断有无条件审核，如果有获取相应数据
        if (conditionDataVoS != null) {
            List<String> fieldList = conditionDataVoS.stream().map(ExamineFlowConditionDataVO::getFieldName).collect(Collectors.toList());
            fieldList.removeIf(StrUtil::isEmpty);
            if (CollUtil.isNotEmpty(fieldList)) {
                ExamineModuleService moduleService = ApplicationContextHolder.getBean(ExamineEnum.parseModule(examineRecord.getLabel()).getServerName());
                dataMap = moduleService.getConditionMap(examineRecord.getLabel(), examineRecord.getTypeId(), examineRecord.getRecordId());
            } else {
                dataMap.put(ExamineConst.CREATE_USER_ID, examineRecord.getCreateUserId());
            }
        }
        examinePreviewBO.setDataMap(dataMap);
        examinePreviewBO.setStatus(1);
        examinePreviewBO.setRecordId(examineRecord.getRecordId());
        examinePreviewBO.setOwnerUserId(examineRecord.getCreateUserId());
        ExaminePreviewVO examinePreviewVO = examineService.previewExamineFlow(examinePreviewBO);
        List<ExamineFlowVO> examineFlowVoS = examinePreviewVO.getExamineFlowList();
        return examineFlowVoS;
    }


    /**
     * 功能描述: <br>
     * 〈〉
     *
     * @param: superExamineRecordVO
     * @param: examineFlowVoS  含有处理人状态的信息
     * @param: examineRecord  全流程信息
     * @return: java.util.List<com.kakarote.examine.entity.VO.ExamineFlowDataVO>
     * @throws:
     * @author: zyh
     * @date: 2022/9/2 16:15
     */
    private List<SuperExamineFlowDataVO> getExamineFlowDataVO(SuperExamineRecordVO superExamineRecordVO, List<ExamineFlowVO> examineFlowVoS, ExamineRecord examineRecord, ExamineFlowFinal flowFinal) {
        List<SuperExamineFlowDataVO> examineFlowDataVoS = new ArrayList<>();

        List<ExamineRecordLog> recordLogList = examineRecordLogService.lambdaQuery()
                .eq(ExamineRecordLog::getRecordId, examineRecord.getRecordId())
                .orderByAsc(ExamineRecordLog::getSort, ExamineRecordLog::getLogId).list();

        Map<Long, List<ExamineRecordLog>> longListMap = recordLogList.stream().collect(Collectors.groupingBy(ExamineRecordLog::getFlowId));

        for (ExamineFlowVO examineFlowVO : examineFlowVoS) {
            SuperExamineFlowDataVO examineFlowDataVO = new SuperExamineFlowDataVO();
            BeanUtil.copyProperties(examineFlowVO, examineFlowDataVO, "userList");
            examineFlowDataVO.setExamineStatus(examineRecord.getExamineStatus());

            List<ExamineRecordLog> list = longListMap.get(examineFlowVO.getFlowId());
            List<ExamineRecordLog> examineRecordLogs = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(list)) {
                examineRecordLogs = list.stream()
                        .filter(recordLog -> !ExamineStatusEnum.CREATE.getStatus().equals(recordLog.getExamineStatus()))
                        .collect(Collectors.toList());
            }

            //当前节点是否自选
            boolean isOptional = false;
            //判断审批流程是否已通过
            boolean isPass = true;
            boolean isCreate = false;
            //审核通过  记录状态
            Integer status = null;

            List<Integer> optionalFlowType = Arrays.asList(ExamineTypeEnum.COPY.getType(), ExamineTypeEnum.OPTIONAL.getType());

            if (optionalFlowType.contains(examineFlowVO.getExamineType())) {
                if (ObjectUtil.isEmpty(examineRecordLogs)) {
                    isOptional = true;
                }
            }


            if (examineRecordLogs.size() > 0) {
                List<ExamineRecordLog> recordLogs = examineRecordLogs.stream()
                        .filter(recordLog -> !ExamineStatusEnum.PASS.getStatus().equals(recordLog.getExamineStatus()))
                        .filter(recordLog -> !ExamineStatusEnum.FORWARD.getStatus().equals(recordLog.getExamineStatus()))
                        .filter(recordLog -> !ExamineStatusEnum.SEEK.getStatus().equals(recordLog.getExamineStatus()))
                        .collect(Collectors.toList());
                if (CollectionUtil.isEmpty(recordLogs)) {
                    isPass = true;
                } else {
                    List<ExamineRecordLog> records = recordLogs.stream()
                            .filter(recordLog -> !ExamineStatusEnum.CREATE.getStatus().equals(recordLog.getExamineStatus()))
                            .collect(Collectors.toList());
                    isCreate = CollectionUtil.isEmpty(records);
                    if (isCreate) {
                        //如果只存在一个创建节点,转发征求他人意见节点则设置false
                        List<ExamineRecordLog> logs = examineRecordLogs.stream()
                                .filter(recordLog -> !ExamineStatusEnum.CREATE.getStatus().equals(recordLog.getExamineStatus()))
                                .filter(recordLog -> !ExamineStatusEnum.FORWARD.getStatus().equals(recordLog.getExamineStatus()))
                                .filter(recordLog -> !ExamineStatusEnum.SEEK.getStatus().equals(recordLog.getExamineStatus()))
                                .collect(Collectors.toList());
                        isPass = !CollectionUtil.isEmpty(logs);
                    } else {
                        //针对或签过滤处理
                        List<ExamineRecordLog> recordValids = examineRecordLogs.stream()
                                .filter(recordLog -> (ExamineStatusEnum.PASS.getStatus().equals(recordLog.getExamineStatus()) && ObjectUtil.isNotEmpty(recordLog.getType()) && recordLog.getType().equals(3)))
                                .collect(Collectors.toList());
                        isPass = CollectionUtil.isNotEmpty(recordValids);

                    }
                }
                ExamineRecordLog recordLog = CollectionUtil.getLast(examineRecordLogs);
                examineFlowDataVO.setPassFlag(recordLog.getPassFlag());
            } else {
                isPass = false;
            }
            if (isPass && !isOptional) {
                status = ExamineStatusEnum.PASS.getStatus();
            }
            List<SimpleUser> userList = examineFlowVO.getFinalUserList();
            //针对空部门空角色下人员处理
            if (CollectionUtil.isEmpty(userList)) {
                List<Long> userIds = examineRecordLogs.stream()
                        .filter(item -> item.getFlowId().equals(examineFlowDataVO.getFlowId()))
                        .filter(item -> !item.getExamineStatus().equals(ExamineStatusEnum.CREATE.getStatus()))
                        .filter(item -> ObjectUtil.isEmpty(item.getRelTarget()))
                        .map(ExamineRecordLog::getExamineUserId)
                        .collect(Collectors.toList());


                if (CollectionUtil.isNotEmpty(userIds)) {
                    List<SimpleUser> finalUserList = new ArrayList<>();
                    userIds.forEach(userId -> {
                        if (ObjectUtil.isNotEmpty(userId)) {
                            SimpleUser simpleUser = new SimpleUser();
                            if (ObjectUtil.equal(userId, 0L)) {
                                simpleUser.setUserId(0L);
                            } else {
                                simpleUser = UserCacheUtil.getSimpleUser(userId);
                            }
                            finalUserList.add(simpleUser);
                        }
                    });
                    userList = finalUserList;
                }
            }
            List<Map<String, Object>> mapList = new ArrayList<>();
            Integer isEnd = null, breakStatus = null, hasPass = null, isAdvance = null, flowType = examineFlowDataVO.getType();
            ExamineAdvancedConfigVO advancedConfigVO = superExamineRecordVO.getExamineAdvancedConfigVO();
            List<SimpleUser> validUsers = new ArrayList<>();
            if (ObjectUtil.isEmpty(userList)) {
                userList = new ArrayList<>();
            } else {
                validUsers = userList.stream()
                        .filter(ObjectUtil::isNotEmpty)
                        .filter(item -> (ObjectUtil.isNotEmpty(item.getUserId()) && ObjectUtil.notEqual(item.getUserId(), 0)) || ObjectUtil.isNotEmpty(item.getOuterUserEmail()))
                        .collect(Collectors.toList());
            }

            if (CollectionUtil.isEmpty(validUsers)) {
                if (!flowFinal.getExamineType().equals(ExamineTypeEnum.COPY.getType())) {
                    Integer nodeHandleType = advancedConfigVO.getNodeHandleType();
                    //如果是通过或者拒绝状态，不需要额外再进行处理
                    boolean isEdit = !examineRecord.getExamineStatus().equals(ExamineStatusEnum.PASS.getStatus()) && !examineRecord.getExamineStatus().equals(ExamineStatusEnum.REJECT.getStatus());
                    if (!isPass && !isCreate && isEdit && !isOptional) {

                        isAdvance = nodeHandleType;
                        status = advancedConfigService.handleRecordLogByAdvancedConfig(examineRecordLogs, flowFinal, false, advancedConfigVO, examineRecord, null);

                    }
                }
            }
            //如果是抄送节点则人员不变化
            if (ExamineTypeEnum.COPY.getType().equals(examineFlowVO.getExamineType())) {
                userList = examineFlowVO.getUserList();
            }
            if (ExamineTypeEnum.OPTIONAL.getType().equals(examineFlowVO.getExamineType()) && CollUtil.isEmpty(userList)) {
                userList = examineFlowVO.getUserList();
            }

            if (CollUtil.isNotEmpty(userList)) {

                List<ExamineRecordLog> deleteList = new ArrayList<>();
                int i = 0;
                for (SimpleUser simpleUser : userList) {
                    Map<String, Object> map = new HashMap<>(6);
                    Long userId = simpleUser.getUserId();
                    map.put("userId", userId);
                    map.put("img", simpleUser.getImg());
                    map.put("realname", simpleUser.getRealname());
                    map.put("outerUserEmail", simpleUser.getOuterUserEmail());
                    if (ObjectUtil.isNotEmpty(simpleUser.getUserId()) && userId != 0L) {
                        UserInfo userInfo = UserCacheUtil.getUserInfo(userId);
                        map.put("userStatus", userInfo.getStatus());
                    } else {
                        map.put("userStatus", 0);
                    }
                    if (NodeHandleTypeEnum.AUTO_PASS.getType().equals(isAdvance) || NodeHandleTypeEnum.FORWARD_OTHERS.getType().equals(isAdvance)) {
                        if (ObjectUtil.notEqual(userId, 0L)) {
                            map.put("isAdvance", isAdvance);
                        }
                    }

                    List<ExamineRecordLog> examineRecordLogList = examineRecordLogs.stream()
                            .filter(e -> !e.getExamineStatus().equals(ExamineStatusEnum.CREATE.getStatus()))
                            .filter(item -> (ObjectUtil.isNotEmpty(userId)
                                    && ObjectUtil.isNotEmpty(item.getExamineUserId())
                                    && item.getExamineUserId().equals(userId))
                                    || (ObjectUtil.isNotEmpty(simpleUser.getOuterUserEmail()) && ObjectUtil.isNotEmpty(item.getExamineUserEmail()) && item.getExamineUserEmail().equals(simpleUser.getOuterUserEmail())))
                            .collect(Collectors.toList()).stream().filter(Objects::nonNull)
                            .filter(examineRecordLog -> {
                                for (ExamineRecordLog recordLog : deleteList) {
                                    if (examineRecordLog.getLogId().equals(recordLog.getLogId())) {
                                        return false;
                                    }
                                }
                                return true;
                            }).collect(Collectors.toList());

                    List<ExamineRecordLog> passRecordList = examineRecordLogs.stream()
                            .filter(item -> (item.getExamineStatus().equals(ExamineStatusEnum.PASS.getStatus())))
                            .collect(Collectors.toList());
                    Long logId = null;
                    LocalDateTime noticeTime;
                    if (CollUtil.isNotEmpty(examineRecordLogList)) {
                        ExamineRecordLog examineRecordLog = examineRecordLogList.get(0);
                        //保存对应审批下的审批记录，若
                        deleteList.add(examineRecordLog);
                        Integer examineStatus = examineRecordLog.getExamineStatus();
                        if (ObjectUtil.isNotEmpty(examineFlowDataVO.getType()) && 3 == examineFlowDataVO.getType() && ExamineStatusEnum.ORINVALID.getStatus().equals(examineStatus)) {
                            continue;
                        }
                        logId = examineRecordLog.getLogId();
                        map.put("examineStatus", examineStatus);
                        if (!ExamineStatusEnum.UNDERWAY.getStatus().equals(examineStatus)) {
                            if (PassFlagEnum.END_AUTO_NO_USER.getType().equals(examineRecordLog.getPassFlag())) {
                                map.put("userStatus", 0);
                            }
                        }
                        if (ObjectUtil.isNotEmpty(advancedConfigVO)) {
                            Integer limitTimeStatus = advancedConfigVO.getLimitTimeStatus();
                            ExamineAdvancedConfigLimitTimeVO advancedLimitTimeVO = advancedConfigVO.getAdvancedLimitTimeVO();
                            if (ObjectUtil.isNotEmpty(limitTimeStatus) && 1 == limitTimeStatus && ObjectUtil.isNotEmpty(advancedLimitTimeVO)) {
                                noticeTime = examineRecordLimitService.getNoticeTime(advancedLimitTimeVO.getLimitTime(), advancedLimitTimeVO.getLimitTimeUnit(), examineRecordLog.getCreateTime());
                                map.put("noticeTime", noticeTime);
                                if ((ExamineStatusEnum.UNDERWAY.getStatus().equals(examineStatus) || (ObjectUtil.isNotEmpty(noticeTime) && noticeTime.isBefore(LocalDateTime.now())))
                                        && (!ExamineStatusEnum.PASS.getStatus().equals(examineStatus)
                                        && !ExamineStatusEnum.SEEK.getStatus().equals(examineStatus)
                                        && !ExamineStatusEnum.END.getStatus().equals(examineStatus)
                                        && !ExamineStatusEnum.ARCHIVE.getStatus().equals(examineStatus)
                                        && !ExamineStatusEnum.COPY.getStatus().equals(examineStatus)
                                        && !ExamineStatusEnum.SKIP.getStatus().equals(examineStatus)
                                        && !ExamineStatusEnum.REMOVE.getStatus().equals(examineStatus)
                                        && !ExamineStatusEnum.INVALID.getStatus().equals(examineStatus)
                                        && !ExamineStatusEnum.RECHECK.getStatus().equals(examineStatus))
                                ) {
                                    Duration duration = Duration.between(examineRecordLog.getExamineTime(), LocalDateTime.now());
                                    map.put("duration", duration.toMillis());
                                }
                            }
                        }

                        map.put("examineTime", examineRecordLog.getExamineTime());
                        map.put("createTime", examineRecordLog.getCreateTime());
                        map.put("remarks", examineRecordLog.getRemarks());
                        //跳过作废的
                        //审核进行中或已结束 记录状态
                        if ((examineStatus != 0 && examineStatus != 1 && examineStatus != 11)) {
                            isEnd = examineStatus;
                        }
                        //审核结束  记录状态
                        if (ListUtil.toList(2, 4, 5, 7, 8, 13, 14).contains(examineStatus)) {
                            if (examineStatus == 4) {
                                //使用发起人信息填充
                                map.put("userId", superExamineRecordVO.getCreateUser().getUserId());
                                map.put("img", superExamineRecordVO.getCreateUser().getImg());
                                map.put("realname", superExamineRecordVO.getCreateUser().getRealname());
                            }
                            breakStatus = examineStatus;
                        }
                        //针对审核状态终止、作废、归档、撤回
                        long count = examineRecordLogList.stream().filter(recordLog -> ListUtil.toList(4, 8, 13, 14).contains(recordLog.getExamineStatus())).count();
                        //针对或签是否存在同意节点
                        if ((examineFlowVO.getType().equals(3) && CollectionUtil.isNotEmpty(passRecordList)) || count > 0) {
                            hasPass = examineStatus;
                        }
                    } else {
                        if (status != null) {
                            map.put("examineStatus", status);
                        } else if (examineFlowVoS.lastIndexOf(examineFlowVO) != examineFlowVoS.size() - 1 && examineRecordLogs.size() == 0) {
                            map.put("examineStatus", ExamineStatusEnum.SKIP.getStatus());
                            map.put("passFlag", examineFlowDataVO.getPassFlag());
                            status = ExamineStatusEnum.SKIP.getStatus();
                        } else {
                            map.put("examineStatus", ExamineStatusEnum.AWAIT.getStatus());
                        }
                    }
                    //如果存在外部联系信息则userStatus为1
                    if (ObjectUtil.isNotEmpty(map.get("outerUserEmail"))) {
                        map.put("userStatus", 1);
                    }

                    mapList.add(map);
                    //审核结束 终止循环人员信息(对于或签拒绝时会产生无用审核人记录，终止循环可以去除多余信息)
                    if (breakStatus != null && hasPass == null) {
                        map.put("examineStatus", breakStatus);
                        break;
                    }

                    /*
                     * 添加转交他人处理节点
                     */
                    if (map.get("examineStatus").equals(ExamineStatusEnum.FORWARD.getStatus()) ||
                            map.get("examineStatus").equals(ExamineStatusEnum.DELEGATE.getStatus())) {
                        if (logId != null) {
                            //转他人处理设置节点的type 为空，不显示依次审批等类型提示
                            if (userList.size() == 1) {
                                flowType = null;
                            }
                            map.put("passFlag", examineRecordLogList.get(0).getPassFlag());
                            //获取当前转交人log信息 //todo 这里根据sort进行判断
                            List<ExamineRecordLog> forwardUserList = examineRecordLogService.lambdaQuery()
                                    .eq(ExamineRecordLog::getRecordId, examineRecord.getRecordId())
                                    .eq(ExamineRecordLog::getFlowId, examineFlowVO.getFlowId())
                                    .in(ExamineRecordLog::getHandlerType, ExamineLogHandlerType.FORWARD.getType(), ExamineLogHandlerType.DELEGATE.getType())
                                    .orderByAsc(ExamineRecordLog::getCreateTime, ExamineRecordLog::getLogId).list();
                            List<ExamineRecordLog> recordLogs = new LinkedList<>();

                            getChildNode(recordLogs, forwardUserList, logId);

                            recordLogs.sort(Comparator.comparing(ExamineRecordLog::getLogId));
                            List<ExamineRecordLog> logList = examineRecordLogList.stream()
                                    .filter(item ->
                                            (!item.getExamineStatus().equals(ExamineStatusEnum.FORWARD.getStatus()) ||
                                                    !item.getExamineStatus().equals(ExamineStatusEnum.DELEGATE.getStatus())

                                            )).collect(Collectors.toList());
                            if (CollectionUtil.isEmpty(logList)) {
                                status = ExamineStatusEnum.AWAIT.getStatus();
                                examineFlowDataVO.setPassFlag(null);
                            }
                            if (CollectionUtil.isNotEmpty(recordLogs)) {
                                List<ExamineRecordLog> underwayLogs = recordLogs.stream().filter(item -> item.getExamineStatus().equals(ExamineStatusEnum.UNDERWAY.getStatus())).collect(Collectors.toList());
                                if (CollectionUtil.isNotEmpty(underwayLogs)) {
                                    status = ExamineStatusEnum.UNDERWAY.getStatus();
                                } else {
                                    List<ExamineRecordLog> passLogs = recordLogs.stream().filter(item -> item.getExamineStatus().equals(ExamineStatusEnum.PASS.getStatus())).collect(Collectors.toList());
                                    if (CollectionUtil.isNotEmpty(passLogs)) {
                                        status = ExamineStatusEnum.PASS.getStatus();
                                    }
                                    List<ExamineRecordLog> invalidLogs = recordLogs.stream().filter(item -> item.getExamineStatus().equals(ExamineStatusEnum.INVALID.getStatus())).collect(Collectors.toList());
                                    if (CollectionUtil.isNotEmpty(invalidLogs)) {
                                        status = ExamineStatusEnum.INVALID.getStatus();
                                    }
                                }
                                recordLogs.stream()
                                        .filter(item -> ObjectUtil.isNotEmpty(item.getCreateTime()))
                                        .sorted(Comparator.comparing(ExamineRecordLog::getCreateTime))
                                        .forEach(
                                                forwardUserLog -> {
                                                    Map<String, Object> forwardUserMap = new HashMap<>(6);
                                                    forwardUserMap.put("userId", forwardUserLog.getExamineUserId());
                                                    if (ObjectUtil.isNotEmpty(forwardUserLog.getExamineUserId())) {
                                                        SimpleUser forWardSimpleUser = UserCacheUtil.getSimpleUser(forwardUserLog.getExamineUserId());
                                                        if (ObjectUtil.isNotEmpty(forWardSimpleUser)) {
                                                            forwardUserMap.put("img", forWardSimpleUser.getImg());
                                                            forwardUserMap.put("realname", forWardSimpleUser.getRealname());
                                                        }
                                                    }
                                                    forwardUserMap.put("outerUserEmail", forwardUserLog.getExamineUserEmail());
                                                    forwardUserMap.put("examineTime", forwardUserLog.getExamineTime());
                                                    forwardUserMap.put("createTime", forwardUserLog.getCreateTime());
                                                    forwardUserMap.put("remarks", forwardUserLog.getRemarks());
                                                    forwardUserMap.put("examineStatus", forwardUserLog.getExamineStatus());
                                                    forwardUserMap.put("handlerType", forwardUserLog.getHandlerType());
                                                    forwardUserMap.put("passFlag", forwardUserLog.getPassFlag());

                                                    mapList.add(forwardUserMap);
                                                }
                                        );
                            }
                        }
                    }
                }
            } else {
                //不是最后一级，节点下没有log审批人为跳过节点
                if (examineFlowVoS.lastIndexOf(examineFlowVO) != examineFlowVoS.size() - 1 && examineRecordLogs.size() == 0) {
                    status = ExamineStatusEnum.SKIP.getStatus();
                }
            }
            //审核进行中或已结束 填充当前流程审核状态
            if (isEnd != null) {
                examineFlowDataVO.setExamineStatus(isEnd);
            } else {
                if (examineFlowVO.getType() != 3) {
                    examineFlowDataVO.setExamineStatus(status != null ? status : ExamineStatusEnum.AWAIT.getStatus());
                } else if (examineRecord.getExamineStatus().equals(ExamineStatusEnum.UNDERWAY.getStatus())) {
                    if (CollectionUtil.isEmpty(examineRecordLogs)) {
                        examineFlowDataVO.setExamineStatus(status != null ? status : ExamineStatusEnum.AWAIT.getStatus());
                    } else {
                        List<ExamineRecordLog> recordLogs = examineRecordLogs.stream()
                                .filter(recordLog -> !ExamineStatusEnum.CREATE.getStatus().equals(recordLog.getExamineStatus()))
                                .collect(Collectors.toList());
                        if (CollectionUtil.isEmpty(recordLogs)) {
                            examineFlowDataVO.setExamineStatus(status != null ? status : ExamineStatusEnum.AWAIT.getStatus());
                        }
                    }

                }
            }
            examineFlowDataVO.setUserList(mapList);
            if (status != null) {
                examineFlowDataVO.setExamineStatus(status);
            }
            examineFlowDataVO.setType(flowType);
            examineFlowDataVoS.add(examineFlowDataVO);
            //审核结束 终止循环剩下的流程
            if ((breakStatus != null || (ExamineStatusEnum.END.getStatus().equals(isEnd))) && hasPass == null) {
                break;
            }
        }
        return examineFlowDataVoS;
    }

    private void getChildNode(List<ExamineRecordLog> childNode, List<ExamineRecordLog> list, Long id) {
        list.stream()
                .filter(item -> ObjectUtil.isNotEmpty(id) && ObjectUtil.isNotEmpty(item.getRelTarget()) && item.getRelTarget().equals(id))
                .forEach(item -> {
                    getChildNode(childNode, list, item.getLogId());
                    childNode.add(item);
                });

    }

    /**
     * 增加创建人节点
     *
     * @param simpleUser simpleUser
     * @return com.kakarote.examine.entity.VO.ExamineFlowDataVO
     * @date 2020/12/22 10:33
     **/
    private SuperExamineFlowDataVO supplementExamineFlowDataVO(SimpleUser simpleUser, LocalDateTime examineTime) {
        SuperExamineFlowDataVO examineFlowDataVO = new SuperExamineFlowDataVO();
        examineFlowDataVO.setFlowId(0L);
        examineFlowDataVO.setExamineStatus(ExamineStatusEnum.CREATE.getStatus());
        Map<String, Object> map = new HashMap<>(6);
        Long userId = simpleUser.getUserId();
        map.put("userId", userId);
        map.put("img", simpleUser.getImg());
        map.put("realname", simpleUser.getRealname());
        map.put("examineStatus", ExamineStatusEnum.CREATE.getStatus());
        map.put("examineTime", examineTime);
        map.put("createTime", examineTime);
        map.put("outerUserEmail", simpleUser.getOuterUserEmail());
        List<Map<String, Object>> userList = ListUtil.toList(map);
        examineFlowDataVO.setUserList(userList);
        return examineFlowDataVO;
    }

    /**
     * 增加征求他人意见节点
     *
     * @param examineFlowDataVoS
     * @return com.kakarote.examine.entity.VO.ExamineFlowDataVO
     * @date 2020/12/22 10:33
     **/
    private List<SuperExamineFlowDataVO> getSeekExamineFlowDataVO(List<SuperExamineFlowDataVO> examineFlowDataVoS, List<ExamineFlowVO> examineFlowVoS, Long recordId) {
        if (CollectionUtil.isEmpty(examineFlowDataVoS)) {
            return examineFlowDataVoS;
        }
        List<SuperExamineFlowDataVO> flowDataVOS = new ArrayList<>();
        //查询待办征求他人意见节点信息
        Map<Long, ExamineFlowVO> flowVOMap = examineFlowVoS.stream().filter(item -> ExamineFlowFlagEnum.SEEK.getType().equals(item.getFlowFlag())).collect(Collectors.toMap(ExamineFlowVO::getFlowId, Function.identity()));
        Map<Long, List<ExamineFlowVO>> flowInsertMap = examineFlowVoS.stream().filter(item -> ObjectUtil.isNotEmpty(item.getFlowId())).collect(Collectors.groupingBy(ExamineFlowVO::getFlowId));
        for (SuperExamineFlowDataVO flowDataVo : examineFlowDataVoS) {
            if (flowVOMap.containsKey(flowDataVo.getFlowId())) {
                ExamineFlowVO examineFlowVO = flowVOMap.get(flowDataVo.getFlowId());
                List<ExamineFlowVO> examineFlowVOS = flowInsertMap.get(examineFlowVO.getInsertTarget())
                        .stream()
                        .sorted(Comparator.comparing(ExamineFlowVO::getCreateTime).reversed())
                        .collect(Collectors.toList());
                ExamineFlowVO flowVO = CollectionUtil.getFirst(examineFlowVOS);
                if (ExamineFlowInsertFlagEnum.BEFORE.getType().equals(examineFlowVO.getInsertFlag())) {//如果当前节点是征求意见节点，且在当前节点之前征求则插入一个临时节点用于页面展示
                    //添加征求他人意见节点
                    flowDataVOS.add(seekExamineFlowDataVO(flowVO, examineFlowVO, recordId));
                }
            }
            flowDataVOS.add(flowDataVo);
        }
        return flowDataVOS;
    }

    /**
     * 增加征求他人意见节点
     *
     * @param
     * @return com.kakarote.examine.entity.VO.ExamineFlowDataVO
     * @date 2020/12/22 10:33
     **/
    private SuperExamineFlowDataVO seekExamineFlowDataVO(ExamineFlowVO sourceFlowVO, ExamineFlowVO seekFlowVO, Long recordId) {
        SuperExamineFlowDataVO examineFlowDataVO = new SuperExamineFlowDataVO();
        examineFlowDataVO.setFlowId(0L);
        examineFlowDataVO.setExamineStatus(ExamineStatusEnum.SEEK.getStatus());
        List<SimpleUser> simpleUsers = sourceFlowVO.getFinalUserList();//原处理节点人员列表

        ExamineFlowFinal aFinal = examineFlowFinalService.lambdaQuery().eq(ExamineFlowFinal::getFlowId, seekFlowVO.getFlowId())
                .eq(ExamineFlowFinal::getRecordId, recordId).one();
        ExamineRecordLog recordLog = examineRecordLogService.getById(aFinal.getRelTargetLogId());

        //查询相应日志信息
        List<ExamineRecordLog> examineRecordLogs = examineRecordLogService.lambdaQuery()
                .eq(ExamineRecordLog::getRecordId, recordId)
                .eq(ExamineRecordLog::getFlowId, seekFlowVO.getFlowId()).orderByDesc(ExamineRecordLog::getCreateTime).list();
        //获取征求他人意见节点日志信息
        List<Map<String, Object>> userList = new ArrayList<>();

        Map<String, Object> userMap = new HashMap<>(6);
        Long userId = recordLog.getExamineUserId();
        SimpleUser simpleUser = UserCacheUtil.getSimpleUser(userId);
        userMap.put("userId", userId);
        userMap.put("img", simpleUser.getImg());
        userMap.put("realname", simpleUser.getRealname());
        userMap.put("examineTime", seekFlowVO.getCreateTime());
        if (CollectionUtil.isNotEmpty(examineRecordLogs)) {
            if (ObjectUtil.isEmpty(seekFlowVO.getCreateTime())) {
                ExamineRecordLog examineRecordLog = CollectionUtil.getFirst(examineRecordLogs);
                userMap.put("examineTime", examineRecordLog.getExamineTime());
            }
        }
        userMap.put("createTime", seekFlowVO.getCreateTime());
        //如果人员id与创建人id相同则设置对应标识状态
        if (userId.equals(seekFlowVO.getCreateUserId())) {
            userMap.put("examineStatus", ExamineStatusEnum.SEEK.getStatus());
            userMap.put("seekUser", true);
        }
        userList.add(userMap);
        examineFlowDataVO.setName(sourceFlowVO.getName() + "·已征求他人意见");
        examineFlowDataVO.setRemarks(seekFlowVO.getInsertRemark());
        examineFlowDataVO.setUserList(userList);
        return examineFlowDataVO;
    }

    public Map<String, String> getOaSqlMap(CrmSearchBO searchBO) {
        Map<String, String> sqlMap = new HashMap<>();
        List<String> oaExamineField = getOaExamineField();
        List<String> wkExamineField = getWkExamineField();
        List<String> wklExamineField = getWkExamineLogField();
        ESConditionToSqlUtil toSqlUtil = new ESConditionToSqlUtil();
        Map<String, String> oaNamePrefixMap = toSqlUtil.getFieldNamePrefixMap(oaExamineField, "oe");
        Map<String, String> wkNamePrefixMap = toSqlUtil.getFieldNamePrefixMap(wkExamineField, "e");
        Map<String, String> wklNamePrefixMap = toSqlUtil.getFieldNamePrefixMap(wklExamineField, "c");
        String oaEsToSql = toSqlUtil.searchESToSql(searchBO, oaNamePrefixMap);
        String wkEsToSql = toSqlUtil.searchESToSql(searchBO, wkNamePrefixMap);
        String wklEsToSql = toSqlUtil.searchESToSql(searchBO, wklNamePrefixMap);
        sqlMap.put("oe", oaEsToSql);
        sqlMap.put("e", wkEsToSql);
        sqlMap.put("c", wklEsToSql);
        return sqlMap;
    }

    public List<String> getOaExamineField() {
        List<String> fields = new ArrayList<>();
        fields.add("content");
        fields.add("create_user_id");
        fields.add("submit_dept_id");
        fields.add("submit_time");
        fields.add("create_time");
        fields.add("examine_status");
        return fields;
    }

    public List<String> getWkExamineField() {
        List<String> fields = new ArrayList<>();
        fields.add("examine_name");
        fields.add("examine_id");
        return fields;
    }

    public List<String> getWkExamineLogField() {
        List<String> fields = new ArrayList<>();
        fields.add("examine_user_id");
        return fields;
    }

    public void setBiTime(ExaminePageBO examinePageBO) {
        if (ObjectUtil.isNotEmpty(examinePageBO.getOriginDateFilter()) || (ObjectUtil.isNotEmpty(examinePageBO.getStartDate()) && ObjectUtil.isNotEmpty(examinePageBO.getEndDate()))) {
            //Bi时间格式转换
            BiParamsUtil.BiTimeEntity biTimeEntity = new BiParamsUtil.BiTimeEntity();
            BiParamsUtil.analyzeDate(biTimeEntity, examinePageBO);
            examinePageBO.setStartTime(biTimeEntity.getBeginDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
            examinePageBO.setEndTime(biTimeEntity.getEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        }
    }


    public List<Map<String, Object>> ExportData(List<Long> collect) {
        List<Map<String, Object>> objectList = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(collect)) {
            objectList = superExamineRecordLogMapper.exportExamineInfo(collect);
        }

        for (Map<String, Object> map : objectList) {

            Object examineId = map.get("examineId");

            String customerNames = "";
            String businessNames = "";
            String contactsNames = "";
            String contractNames = "";
            String receivablesNames = "";
            String productNames = "";


            Integer examineStatus = TypeUtils.castToInt(map.get("examineStatus"));

            switch (examineStatus) {
                case 0:
                    map.put("examineStatus", "未审核");
                    break;
                case 1:
                    map.put("examineStatus", "审核通过");
                    break;
                case 2:
                    map.put("examineStatus", "审核拒绝");
                    break;
                case 3:
                    map.put("examineStatus", "审核中");
                    break;
                case 4:
                    map.put("examineStatus", "已撤回");
                    break;
                case 5:
                    map.put("examineStatus", "未提交");
                    break;
                default:
                    map.put("examineStatus", "");
            }

            /**
             * 获取自定义字段数据
             */
            Result<List<Map<String, Object>>> field = oaService.getFieldInfo(new HashMap<String, Object>() {{
                put("isDetail", 1);
                put("examineId", examineId);
                put("type", 1);
            }});

            map.put("fieldList", field.getData());


            //获取关联用户信息
            Result<Map<Integer, Set<Long>>> relationById = oaService.getRelationById(Long.valueOf(String.valueOf(map.get("examineId"))));
            Map<Integer, Set<Long>> data = relationById.getData();

            if (ObjectUtil.isNotEmpty(data)) {
                CrmRelationDTO crmRelationDTO = CrmRelationUtils.getCrmRelation(data, Long.valueOf(String.valueOf(map.get("examineId"))));

                customerNames = crmRelationDTO.getCustomerList().stream().map(SimpleCrmEntity::getName).collect(Collectors.joining(","));
                businessNames = crmRelationDTO.getBusinessList().stream().map(SimpleCrmEntity::getName).collect(Collectors.joining(","));
                contactsNames = crmRelationDTO.getContactsList().stream().map(SimpleCrmEntity::getName).collect(Collectors.joining(","));
                contractNames = crmRelationDTO.getContractList().stream().map(SimpleCrmEntity::getName).collect(Collectors.joining(","));
                receivablesNames = crmRelationDTO.getReceivablesList().stream().map(SimpleCrmEntity::getName).collect(Collectors.joining(","));
                productNames = crmRelationDTO.getProductList().stream().map(SimpleCrmEntity::getName).collect(Collectors.joining(","));

            }

            map.put("customerNames", customerNames);
            map.put("businessNames", businessNames);
            map.put("contactsNames", contactsNames);
            map.put("contractNames", contractNames);
            map.put("receivablesNames", receivablesNames);
            map.put("productNames", productNames);

        }
        return objectList;

    }
}
